package cn.iocoder.yudao.module.bpm.service.sourceapplydetail;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.bpm.controller.admin.sourceapplydetail.vo.SourceApplyDetailPageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.sourceapplydetail.vo.SourceApplyDetailSaveReqVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.sourceapplydetail.SourceApplyDetailDO;
import cn.iocoder.yudao.module.bpm.dal.mysql.sourceapplydetail.SourceApplyDetailMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.*;

/**
 * 资源申请详情 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class SourceApplyDetailServiceImpl implements SourceApplyDetailService {

    @Resource
    private SourceApplyDetailMapper sourceApplyDetailMapper;

    @Override
    public Long createSourceApplyDetail(SourceApplyDetailSaveReqVO createReqVO) {
        // 插入
        SourceApplyDetailDO sourceApplyDetail = BeanUtils.toBean(createReqVO, SourceApplyDetailDO.class);
        sourceApplyDetailMapper.insert(sourceApplyDetail);
        // 返回
        return sourceApplyDetail.getId();
    }

    @Override
    public void updateSourceApplyDetail(SourceApplyDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateSourceApplyDetailExists(updateReqVO.getId());
        // 更新
        SourceApplyDetailDO updateObj = BeanUtils.toBean(updateReqVO, SourceApplyDetailDO.class);
        sourceApplyDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteSourceApplyDetail(Long id) {
        // 校验存在
        validateSourceApplyDetailExists(id);
        // 删除
        sourceApplyDetailMapper.deleteById(id);
    }

    private void validateSourceApplyDetailExists(Long id) {
        if (sourceApplyDetailMapper.selectById(id) == null) {
            throw exception(SOURCE_APPLY_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public SourceApplyDetailDO getSourceApplyDetail(Long id) {
        return sourceApplyDetailMapper.selectById(id);
    }

    @Override
    public PageResult<SourceApplyDetailDO> getSourceApplyDetailPage(SourceApplyDetailPageReqVO pageReqVO) {
        return sourceApplyDetailMapper.selectPage(pageReqVO);
    }

    @Override
    public List<SourceApplyDetailDO> getSourceApplyDetailList(Long applyId) {
        return sourceApplyDetailMapper.getSourceApplyDetailList(applyId);
    }

}